package com.exam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.exam.exception.ScheduleIsExistException;
import com.exam.exception.StudentNonExist;
import com.exam.pojo.Major;
import com.exam.pojo.Paging;
import com.exam.pojo.Schedule;
import com.exam.pojo.Student;
import com.exam.pojo.Subject;
import com.exam.service.ClassesService;
import com.exam.service.MajorService;
import com.exam.service.ScheduleService;
import com.exam.service.StudentService;
import com.exam.service.SubjectService;
import com.google.gson.Gson;

/**
 * @author 郭荣锋
 * 考试安排管理
 */
@Controller
@RequestMapping("/ScheduleManage")
public class ScheduleManage {

	@Resource(name="mv")
	private ModelAndView mv;
	@Resource(name="studentServiceImpl")
	private StudentService studentService;
	@Resource(name="majorServiceImpl")
	private MajorService majorService;	
	@Resource(name="classesServiceImpl")
	private ClassesService classesService;
	@Resource(name="student")
	private Student student;
	@Resource(name="gson")
	private Gson gson;
	@Resource(name="map")
	private Map map;
	@Resource(name="subjectServiceImpl")
	private SubjectService subjectService;
	@Resource(name="scheduleServiceImpl")
	private ScheduleService scheduleService;
	
	/**
	 * 考试安排列表
	 * @return
	 */
	@RequestMapping("/schedule-list")
	public ModelAndView examList(HttpServletRequest request,HttpServletResponse response) {
		List<Major> majorList = majorService.getAllMajor(); //专业列表
		mv.addObject("majorList",majorList);
		mv.setViewName("admin/schedule-list");
		return mv;
	}
	
	/**
	 * 已取消的考试页面
	 * @return
	 */
	@RequestMapping("/schedule-cancel")
	public ModelAndView examCancel(HttpServletRequest request,HttpServletResponse response) {
		List<Major> majorList = majorService.getAllMajor(); //专业列表
		mv.addObject("majorList",majorList);
		mv.setViewName("admin/schedule-cancel");
		return mv;
	}
	
	/**
	 * 已过期的考试
	 * @return
	 */
	@RequestMapping("/schedule-expired")
	public ModelAndView examExpired(HttpServletRequest request,HttpServletResponse response) {
		List<Major> majorList = majorService.getAllMajor(); //专业列表
		mv.addObject("majorList",majorList);
		mv.setViewName("admin/schedule-expired");
		return mv;
	}
	
	/**
	 * 未开考的考试页面
	 * @return
	 */
	@RequestMapping("/schedule-before")
	public ModelAndView examBefore(HttpServletRequest request,HttpServletResponse response) {
		List<Major> majorList = majorService.getAllMajor(); //专业列表
		mv.addObject("majorList",majorList);
		mv.setViewName("admin/schedule-before");
		return mv;
	}
	
	@RequestMapping("/schedule-add")
	public ModelAndView schedule_add(HttpServletRequest request,HttpServletResponse response) {
		List<Major> majorList = majorService.getAllMajor(); //专业列表
		mv.addObject("majorList",majorList);
		List<Subject> subjectList = subjectService.getAllSubject();
		mv.addObject("subjectList",subjectList);
		mv.setViewName("admin/schedule-add");
		return mv;
	}
	
	/**
	 * 添加考试
	 * @throws IOException 
	 */
	@RequestMapping("/addSchedule")
	public void addSchedule(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		
		String dates = request.getParameter("dates"); //考试时间
		String classesName = request.getParameter("classesName"); //班级名称
		String majorName = request.getParameter("majorName"); //专业名称
		String subjectName = request.getParameter("subjectName"); //学科名称
		try {
			scheduleService.addSchedule(subjectName, classesName, majorName, dates);
			printWriter.write("{\"res\":\"1\"}");
		} catch (ScheduleIsExistException e) {
			printWriter.write("{\"res\":\"0\"}");
		}
		printWriter.close();
	}
	
	/**
	 * 获取考试安排列表
	 * @throws IOException 
	 */
	@RequestMapping("/getSchedule")
	public void getSchedule(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		String subjectName = request.getParameter("subject");
		String classesName = request.getParameter("");
		Integer page =Integer.parseInt(request.getParameter("page"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));		
		Integer state = Integer.parseInt(request.getParameter("state")); //状态
		Map map = new HashMap();
		map.put("page",page);
		map.put("pageSize",pageSize);
		map.put("state",state);
		Paging<Schedule> paging = scheduleService.getSchedule(map);
		String s = gson.toJson(paging);
		printWriter.write(s);
		printWriter.close();
	}
	
	/**
	 * 删除考试安排
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/delete")
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer scheduleId = Integer.parseInt(request.getParameter("scheduleId"));
		scheduleService.deleteSchedule(scheduleId);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 批量删除考试安排
	 * @throws IOException 
	 */
	@RequestMapping("/deleteByQuery")
	public void deleteByQuery(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String list = request.getParameter("scheduleIdList");
		String[] list1 = gson.fromJson(list,String[].class); //获取要删除的学生编号
		for(String scheduleId:list1) { //循环遍历删除
			Schedule schedule = scheduleService.getScheduleById(Integer.parseInt(scheduleId));
			scheduleService.deleteSchedule(schedule.getId());
		}
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
}
